require(stats)
require(graphics)
#library("ggplot2")
require(ggplot2)
#library("grDevices")
require(grDevices)
#library("psych")
require(psych)
#library("lattice")
require(lattice)

# Please make sure to change the path to where the appropriate dataset is located on your computer

sc.sens.gb <- read.csv("PNAS_VAL_GB.csv")
sc.sens.ij.nochan <- read.csv("PNAS_VAL_IJ_NOCHAN.csv")
sc.sens.ij.closed <- read.csv("PNAS_VAL_IJ_CLOSED.csv")


# Columns
sponsor.require <- 1
hs.p <-  2
ls.p <- 3
p.tourist <- 4
p.fakedocs <- 5
no.econ.mig.stud <- 6
learn.i.coeff <- 7
learn.j.coeff <- 8
learn.good.coeff <- 9
learn.bad.coeff <- 10
my.seed <- 11

legaltourist.attempts <- c(12:31)

ability.legal <- c(32:92)
ability.fakedocs <- c(93:153)
ability.work <- c(154:214)

tot.mig <- c(215:275)
legal.mig <- c(276:336)
fd.mig <- c(337:397)
ut.mig <- c(398:458)
stud.mig <- c(459:519)
hs.mig <- c(520:580)
ls.mig <- c(581:641)
fam.mig <- c(642:702)


## GOOD/BAD

# Volume 

sc.sens.gb[,tot.mig[12]]

means.tot.mig.gb <- aggregate((sc.sens.gb[,tot.mig[12]] / 1166), 
                              by = list(sc.sens.gb[,learn.bad.coeff], 
                                        sc.sens.gb[,learn.good.coeff]), mean)

vt.gb.val <- as.vector(means.tot.mig.gb$x)

dim(vt.gb.val) <- c(11, 11)

x <- seq(from = 0, to = 1, by= 0.1)
r <- seq(from = 0, to = 1, by= 0.1)

pdf("FigD2a.pdf",width=6,height=6)
par(mar = c(5, 4, 4, 2) + 0.1)
filled.contour(x,r,vt.gb.val, xlab = "Negative Signals Weight", 
               ylab = "Positive Signals Weight", color.palette = topo.colors) 
dev.off()

# Unauthorized migration

sc.sens.gb$illegal.mig <- sc.sens.gb[,ut.mig[12]] + sc.sens.gb[,fd.mig[12]]

means.ill.mig.gb <- aggregate((sc.sens.gb$illegal.mig / 1166), 
                              by = list(sc.sens.gb[,learn.bad.coeff], 
                                        sc.sens.gb[,learn.good.coeff]), mean)


vi.gb.val <- as.vector(means.ill.mig.gb$x)

dim(vi.gb.val) <- c(11, 11)

x <- seq(from = 0, to = 1, by= 0.1)
r <- seq(from = 0, to = 1, by= 0.1)

pdf("FigD2b.pdf",width=6,height=6)
par(mar = c(5, 4, 4, 2) + 0.1)
filled.contour(x,r,vi.gb.val, xlab = "Negative Signals Weight", 
               ylab = "Positive Signals Weight", color.palette = topo.colors) 
dev.off()

# SELF/NETWORK: VOLUME

# FREE MOVEMENT

means.tot.mig.nochan <- aggregate((sc.sens.ij.nochan[,tot.mig[12]] / 1166), by = list(sc.sens.ij.nochan[,learn.i.coeff], 
                                                               sc.sens.ij.nochan[,learn.j.coeff]), mean)

vt.ij.nochan <- as.vector(means.tot.mig.nochan$x)

dim(vt.ij.nochan) <- c(11, 11)

x <- seq(from = 0, to = 1, by= 0.1)
r <- seq(from = 0, to = 1, by= 0.1)

pdf("FigD3b.pdf",width=6,height=6)
par(mar = c(5, 4, 4, 2) + 0.1)
filled.contour(x,r,vt.ij.nochan, xlab = "Personal Learning Weight", ylab = "Learning through Networks Weight", 
               color.palette = topo.colors) 
dev.off()


# CLOSED

means.tot.mig.closed <- aggregate((sc.sens.ij.closed[,tot.mig[12]] / 1166), 
                                  by = list(sc.sens.ij.closed[,learn.i.coeff], 
                                  sc.sens.ij.closed[,learn.j.coeff]), mean)


vt.ij.closed <- as.vector(means.tot.mig.closed$x)

dim(vt.ij.closed) <- c(11, 11)

x <- seq(from = 0, to = 1, by= 0.1)
r <- seq(from = 0, to = 1, by= 0.1)

pdf("FigD3a.pdf",width=6,height=6)
par(mar = c(5, 4, 4, 2) + 0.1)
filled.contour(x,r,vt.ij.closed, xlab = "Personal Learning Weight", ylab = "Learning through Networks Weight", 
               color.palette = topo.colors) 
dev.off()


# SELF/ NETWORK: UNAUTHORIZED MIGRATION

# FREE MOVEMENT

sc.sens.ij.nochan$illegal.mig <- sc.sens.ij.nochan[,ut.mig[12]] + sc.sens.ij.nochan[,fd.mig[12]]


means.ill.mig.nochan <- aggregate((sc.sens.ij.nochan$illegal.mig / 1166), 
                                  by = list(sc.sens.ij.nochan[,learn.i.coeff], 
                                      sc.sens.ij.nochan[,learn.j.coeff]), mean)

vi.ij.nochan <- as.vector(means.ill.mig.nochan$x)

dim(vi.ij.nochan) <- c(11, 11)

x <- seq(from = 0, to = 1, by= 0.1)
r <- seq(from = 0, to = 1, by= 0.1)

pdf("FigD4b.pdf",width=6,height=6)
par(mar = c(5, 4, 4, 2) + 0.1)
filled.contour(x,r,vi.ij.nochan, xlab = "Personal Learning Weight", ylab = "Learning through Networks Weight", 
               color.palette = topo.colors) 
dev.off()

# CLOSED

sc.sens.ij.closed$illegal.mig <- sc.sens.ij.closed[,ut.mig[12]] + sc.sens.ij.closed[,fd.mig[12]]



means.ill.mig.closed <- aggregate((sc.sens.ij.closed$illegal.mig / 1166), 
                                  by = list(sc.sens.ij.closed[,learn.i.coeff], 
                                            sc.sens.ij.closed[,learn.j.coeff]), mean)


vi.ij.closed <- as.vector(means.ill.mig.closed$x)

dim(vi.ij.closed) <- c(11, 11)

x <- seq(from = 0, to = 1, by= 0.1)
r <- seq(from = 0, to = 1, by= 0.1)

pdf("FigD4a.pdf",width=6,height=6)
par(mar = c(5, 4, 4, 2) + 0.1)
filled.contour(x,r,vi.ij.closed, xlab = "Personal Learning Weight", ylab = "Learning through Networks Weight", 
               color.palette = topo.colors) 

dev.off()

